﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace GZip
{
    class Program
    {

        void unzip(FileInfo f)
        {
            string ext = f.Extension;

            if (ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }


            if (ext.Equals(".bmp", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }


            if (ext.Equals(".png", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }


            if (ext.Equals(".gif", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }


            if (ext.Equals(".jpeg", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (ext.Equals(".mp3", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }



            var data = File.ReadAllBytes(f.FullName);
            byte[] result = null;

            bool nodata = true;


            if (nodata)
            {
                try
                {
                    result = Common.GZipHelper.GZipDecompress(data);

                    if (result == null)
                    {
                        nodata = true;
                    }
                    else
                    {
                        if (result.Length == 0)
                        {
                            nodata = true;
                        }
                        else
                        {
                            nodata = false;
                        }
                    }
                }
                catch (Exception ex)
                {

                    Console.WriteLine("::" + ex.Message);
                }
            }


            if (nodata)
            {
                try
                {
                    result = Common.GZipHelper.ParseChunkedData(data);

                    if (result == null)
                    {
                        nodata = true;
                    }
                    else
                    {
                        if (result.Length == 0)
                        {
                            nodata = true;
                        }
                        else
                        {
                            nodata = false;
                        }
                    }
                }
                catch (Exception exx)
                {
                    Console.WriteLine(":::" + exx.Message);

                }

            }




            if (!nodata)
            {
                if (result.Length > 0)
                {
                    var fpath = f.FullName;

                    f.MoveTo(fpath + ".bak");


                    using (var fs = File.Create(fpath))
                    {

                        fs.Write(result, 0, result.Length);
                        fs.Close();
                    }

                    File.Delete(fpath + ".bak");


                    Console.WriteLine(fpath);
                }

            }
        }

        public void Run(DirectoryInfo dir)
        {
            var files = dir.GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                var f = files[i];

                unzip(f);
            }

            var dirs = dir.GetDirectories();

            for (int i = 0; i < dirs.Length; i++)
            {
                Run(dirs[i]);
            }
        }
        public void Run(string[] args)
        {
            DirectoryInfo dir = null;
            if (args.Length == 1)
            {
                dir = new DirectoryInfo(args[0]);
            }
            else if (args.Length == 0)
            {
                dir = new DirectoryInfo(System.Environment.CurrentDirectory);
            }


            if (dir != null)
            {
                Run(dir);
            }
        }

        static void Main(string[] args)
        {
            new Program().Run(args);
        }
    }
}
